﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections;
using System.Linq;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>

    public partial class YCAFDPLAN
    { 
        public void AUTOEK()
        {
            DataSet SResult;
            SResult = m_Data.CreateTempEKPOS(SetTmp.ICC_EKPOS);
            tpEKPOS.Clear();
            SetTmp.Clear();
            Set4ITX.ICC_EKPOS.Clear();
            Set4ITX.Merge(SResult);
            btnITXPurchase.IsEnabled = true;
        }
        public void LOADXDECK(DataRow dwInput, DataRow dwSrc,bool isPlanned, string sections, bool isClearEKPOS = true)
        {

            txtWarn.Text = "";
            DataSet SResult;
            SResult = m_Data.LoadXDECKData(dwInput, isPlanned, sections, EKPBEKZ);
            SetData.ABPOS.Clear();
            cmbKRS.Text = "FP";
            foreach (DataRow rx in SResult.Tables["ICC_ABAUFM"].Rows)
            {
                decimal qtyre = System.Convert.ToDecimal(rx["ABAQTYRECEIVED"].ToString());
                if (qtyre > 0)//通过此标志，判断是否是补充采购。
                {
                    cmbKRS.Text = "RP";
                    break;
                }
            }
            //MBST EKPOS MDECK ABPOS ICC_ABAUFM
            if (SResult.Tables.Contains("ICC_STOCK"))
            {
                Set4ITX.Merge(SResult.Tables["ICC_STOCK"]);
            }

            if (SResult.Tables.Contains("ABPOS"))
            {
                SetData.ABPOS.Clear();
                SetData.Merge(SResult.Tables["ABPOS"]);
            }
            if (SResult.Tables.Contains("ICC_ABAUFM"))
            {
                SetData.ICC_ABAUFM.Clear();
                SetData.Merge(SResult.Tables["ICC_ABAUFM"]);
            }
            if (SResult.Tables.Contains("NC_ICC_ABAUFM"))
            {
                if (SetData.Tables.Contains("NC_ICC_ABAUFM"))
                {
                    SetData.Tables["NC_ICC_ABAUFM"].Clear();
                }
                SetData.Merge(SResult.Tables["NC_ICC_ABAUFM"]);
                if (SetData.Tables["NC_ICC_ABAUFM"].Rows.Count > 0)
                {
                    txtWarn.Text = "请注意相关的款式主面料尚未确认！";
                }
            }
            if (vwITXXDECK.View != null)
            {
                vwITXXDECK.View.MoveCurrentToFirst();
            }
            if (isClearEKPOS)
            {
                Set4ITX.ICC_STOCK.Clear();
                Set4ITX.ICC_EKPOS.Clear();
                if (SResult.Tables.Contains("ICC_EKPOS"))
                {
                    Set4ITX.Merge(SResult.Tables["ICC_EKPOS"]);
                }
            }
            else
            {
                if (SResult.Tables.Contains("ICC_EKPOS"))
                {
                    if (SResult.Tables["ICC_EKPOS"].Rows.Count > 0)
                    {
                        var qekp = from DataRow row in SResult.Tables["ICC_EKPOS"].AsEnumerable()
                                    where row["EKPAUFKRS"].ToString() == dwSrc["EKPAUFKRS"].ToString()
                                    && row["EKPAUFLFD"].ToString() == dwSrc["EKPAUFLFD"].ToString()
                                    && row["EKPAUFPOS"].ToString() == dwSrc["EKPAUFPOS"].ToString()
                                    select row;

                        foreach(DataRow row in qekp)
                        {
                            dwSrc["EKPQTYLOCKED"] = row["EKPQTYLOCKED"];
                            dwSrc["EKPQTYFREE"] = row["EKPQTYFREE"];
                            continue;
                        }
                    }
                }
            }
            /*
            txtExtFactory.Text = "";
            var query = from row in SetData.Tables["ICC_ABAUFM"].AsEnumerable()
                        group row by row.Field<string>("LSLIEFNA") into grp
                        select new
                        {
                            Id = grp.Key,
                            sum = grp.Sum(r => r.Field<decimal>("ABARESMG"))
                        };
            foreach (var grp in query)
            {
                txtExtFactory.Text += grp.Id + grp.sum + Environment.NewLine;
            }*/
        }
    }
}